
#include <stdio.h>
#include "GL/glew.h"

#include "land_graphics.h"

#define LAND_OGL_CORE

#include "init.h"

static struct {
	LandDepthTest depthTest;
}ctx = { 0 };

static void
bindDraw(LandCanvas* canvas, LandDraw* draw) {
	landBindOglCanvas(canvas);
	landBindOglVertex(draw->vertex);
	landBindOglShader(canvas, draw);
}

static void
drawPoints(LandCanvas* canvas, LandFloat size, LandDraw* draw){
	bindDraw(canvas, draw);
	glPointSize((GLfloat)size);
	void* offset = (void*)(sizeof(unsigned) * draw->offset);
	glDrawElements(GL_POINTS,
		draw->count, GL_UNSIGNED_INT, offset);
}

static void
draw(LandCanvas* canvas, LandDraw* draw) {
	bindDraw(canvas, draw);
	GLint type = LAND_DRAW_TRIANGLES;
	switch (draw->drawType) {
	case LAND_DRAW_POINTS:
		type = GL_POINTS; break;
	case LAND_DRAW_LINES:
		type = GL_LINES; break;
	case LAND_DRAW_LINE_STRIP:
		type = GL_LINE_STRIP; break;
	case LAND_DRAW_LINE_LOOP:
		type = GL_LINE_LOOP; break;
	case LAND_DRAW_TRIANGLES:
		type = GL_TRIANGLES; break;
	case LAND_DRAW_TRIANGLE_STRIP:
		type = GL_TRIANGLE_STRIP; break;
	case LAND_DRAW_TRIANGLE_FAN:
		type = GL_TRIANGLE_FAN; break;
	}
	void* offset = (void*)(sizeof(unsigned) * draw->offset);
	glDrawElements(type,
		draw->count, GL_UNSIGNED_INT, offset);
	GLint err = glGetError();
	if (err) {
		printf("draw err:%d\n", err);
	}
}


static LandDrawBackend backend = {
	drawPoints,
	draw,
};

LandDrawBackend*
landGetOglDrawBackend(){
    return &backend;
}

int
landInitOglDrawContext(LandGraphics* config, LandOgl* oglConfig) {
	return 0;
}